<template>
    <div class="app-container">
      <el-card shadow="always">
        <!--条件查询带分页-->
        <el-form :inline="true" :model="studentSearch" class="demo-form-inline" align="left" size="small">
          {{studentSearch}}
          <el-form-item>
            <el-input v-model="studentSearch.sname" placeholder="学生姓名"/>
          </el-form-item>
          <el-form-item>
            <el-input v-model="studentSearch.addr" placeholder="学生住址"/>
          </el-form-item>
          <el-form-item>
            <el-select v-model="studentSearch.cid" placeholder="所在班级">
              <el-option label="所有班级" value="0"></el-option>
              <el-option :label="c.cname" :value="c.cid" v-for="c in classes" :key="c.cid"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="search" plain>搜索</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="success" icon="el-icon-plus" @click="addStud" plain>添加学生</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :data="students"
          style="width: 100%">
          <el-table-column
            label="学号"
            align="center"
            prop="sid"
            width="180"/>

          <el-table-column
            label="姓名"
            align="center"
            prop="sname"/>
          <el-table-column
            label="性别"
            align="center"
            prop="sex"/>
          <el-table-column
            label="年龄"
            align="center"
            prop="age"/>
          <el-table-column
            label="住址"
            align="center"
            prop="addr"/>
          <el-table-column
            label="所在班级"
            align="center"
            prop="cname"/>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          align="right"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-card>
    </div>
</template>

<script>
  import studentApi from '@/api/students'
  import classesApi from '@/api/classes'
  export default {
    data() {
      return {
        students: [],
        page:1,
        pageSize:5,
        total:100,
        classes:[],   // 所有班级
        studentSearch:{},

      }
    },
    created() {
      //1. 查询所有学生
      // this.findStudents()
      //2. 查询所有班级
      this.findClasses()
      //3. 分页带条件查询
      this.search()
    },
    methods: {
      // 到添加学生页面
      addStud(){
        this.$router.push("/student/edit")
      },
      //1. 查询所有学生
      findStudents(){
        studentApi.findStudens().then(({data}) => {
          console.log("data:",data)
          this.students = data.list
        })
      },
      // 2. 查询所有班级
      findClasses(){
        classesApi.findClasses().then(({data}) => {
          this.classes = data.list
        })
      },
      // 3. 条件查询带分页
      search(){
        studentApi.search(this.page,this.pageSize,this.studentSearch).then(({data}) => {
            console.log("data:",data)
            this.students = data.pageResult.list
            this.total = data.pageResult.total
        })
      },
      // pageSize大小改变时触发
      handleSizeChange(pageSize) {
        this.pageSize = pageSize
        this.search()
        console.log(`每页 ${pageSize} 条`);
      },
      // page大小发生改变时触发
      handleCurrentChange(page) {
        this.page = page
        this.search()
        console.log(`当前页: ${page}`);
      },
      // 编辑学生
      handleEdit(index, row) {
        console.log(row.sid)
        this.$router.push("/student/edit/"+row.sid)
      },
      handleDelete(index, row) {
        console.log(index, row);
      }
    }
  }
</script>

<style scoped>
  .el-pagination{
    margin-top: 20px;
  }
</style>
